var currentPage = 0
/*
currentSearch == -3 初始化搜索
currentSearch == -1 全部搜索
currentSearch == -2 条件搜索/
 */
var currentSearch = -1

$(document).ready(function () {
    addTimeOutAction()
    initTemporaryApply()
})
/*
初始化申请记录/
 */
function initTemporaryApply(page_ = 0) {
    currentSearch = -3
    currentPage = page_
    var page = currentPage
    var sortFieldName = 'applyTime'
    var asc = 0
    var size = 10
    var id = window.localStorage.userID
    var startDate = '2000-01-01'
    var endDate = '2000-01-01'
    var status = 0
    $.ajax({
        url: ipPort + '/temporaryProjectHeader/getByAuditorAndStatusAndDateByPage?page=' + page + '&sortFieldName='
            + sortFieldName + '&asc=' + asc + '&size=' + size + '&startDate=' + startDate + '&endDate=' + endDate
            + '&auditorId=' + id + '&status=' + status,
        success:function (obj) {
            if(obj.code == 0){
                setTemporaryProjectTable(obj)
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
获取所有申请记录/
 */
function getAllTemporaryApply(page_ = 0) {
    currentSearch = -1
    currentPage = page_
    var page = currentPage
    var sortFieldName = 'applyTime'
    var asc = 0
    var size = 10
    var id = window.localStorage.userID
    var startDate = '2000-01-01'
    var endDate = '2000-01-01'
    var status = -1
    $.ajax({
        url: ipPort + '/temporaryProjectHeader/getByAuditorAndStatusAndDateByPage?page=' + page + '&sortFieldName='
            + sortFieldName + '&asc=' + asc + '&size=' + size + '&startDate=' + startDate + '&endDate=' + endDate
            + '&auditorId=' + id + '&status=' + status,
        success:function (obj) {
            if(obj.code == 0){
                setTemporaryProjectTable(obj)
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
通过状态搜索/
 */
function searchByStatus(page_ = 0) {
    currentSearch = -2
    currentPage = page_
    var sortFieldName = 'applyTime'
    var asc = 0
    var size = 10
    var startDate = '2000-01-01'
    var endDate = '2000-01-01'
    var auditorId = window.localStorage.userID
    var status = $('#selectStatus-dropdownMenu').attr('value')
    $.ajax({
        url:ipPort + '/temporaryProjectHeader/getByAuditorAndStatusAndDateByPage?status=' + status + '&page=' + currentPage + '&sortFieldName='
            + sortFieldName + '&asc=' + asc + '&size=' + size + '&startDate=' + startDate + '&endDate=' + endDate + '&auditorId=' + auditorId,
        success:function (obj) {
            if(obj.code == 0){
                if(obj.data.numberOfElements == 0){
                    alert('无相关信息！')
                    setTemporaryProjectTable(obj)
                }else {
                    setTemporaryProjectTable(obj)
                }
            }else{
                alert(obj.message)
            }
        },
        error:function (error) {
            consle.log(error)
        }
    })
}
/*
设置申请表/
 */
function setTemporaryProjectTable(obj) {
    $('.currentPage').text(currentPage + 1)
    $('.totalPage').text(obj.data.totalPages)
    var id = $('.temporaryProjectAudit-id')
    var applyUser = $('.temporaryProjectAudit-applyUser')
    var department = $('.temporaryProjectAudit-department')
    var workDate = $('.temporaryProjectAudit-workDate')
    var applyTime = $('.temporaryProjectAudit-applyTime')
    var auditor = $('.temporaryProjectAudit-auditor')
    var status = $('.temporaryProjectAudit-status')
    var tr = $('.table-tr')
    var length = obj.data.numberOfElements
    for(var i = 0; i < length; i++){
        tr.eq(i).removeClass('hidden')
        id.eq(i).text('')
        applyUser.eq(i).attr('value', '')
        applyUser.eq(i).text('')
        department.eq(i).attr('value', '')
        department.eq(i).text('')
        workDate.eq(i).text('')
        applyTime.eq(i).text('')
        status.eq(i).attr('value', '')
        status.eq(i).text('')

        id.eq(i).text(obj.data.content[i].id)
        applyUser.eq(i).attr('value', obj.data.content[i].applicant.id)
        applyUser.eq(i).text(obj.data.content[i].applicant.name)
        department.eq(i).attr('value', obj.data.content[i].department.id)
        department.eq(i).text(obj.data.content[i].department.name)
        workDate.eq(i).text(obj.data.content[i].date)
        applyTime.eq(i).text(obj.data.content[i].applyTime)
        auditor.eq(i).text(obj.data.content[i].auditor.name)
        status.eq(i).attr('value', obj.data.content[i].status)
        if(obj.data.content[i].status == 0){
            status.eq(i).text('未审核')
        } else if(obj.data.content[i].status == 1){
            status.eq(i).text('通过')
        }else if(obj.data.content[i].status == 2){
            status.eq(i).text('未通过')
        }
    }
    for(var i = length; i < 10; i++){
        tr.eq(i).addClass('hidden')
    }
}
/*
设置申请表详情Modal/
 */
function setTemporaryProjectAuditRecordModal(thisObj) {
    $('#makeTablePerson').text('')
    $('#makeTablePerson').attr('value', '')
    $('#workDate').text('')
    $('#department').text('')
    $('#applyUser').text('')
    $('#auditor').text('')
    $('#status').text('')
    $('#serviceCharge').text('')
    $('#personNumber').text('')
    $('#content').val('')
    $('.temporaryProjectManagerDetails-table-selfDefine').find('.table-tr').remove()
    $('#totalWage').text('')
    $('#note').text('')
    $('#auditor2').text('')
    $('#auditTime').text('')
    var id = $(thisObj).parent().parent().find('td').eq(0).text()
    $.ajax({
        url: ipPort + '/temporaryProjectHeader/getById?id=' + id,
        success: function (obj) {
            if(obj.code == 0){
                $('#makeTablePerson').text(obj.data.operator.name)
                $('#makeTablePerson').attr('value', obj.data.id)
                $('#workDate').text(obj.data.date)
                $('#department').text(obj.data.department.name)
                $('#applyUser').text(obj.data.applicant.name)
                $('#auditor').text(obj.data.auditor.name)
                if(obj.data.status == 0){
                    $('#status').text('未审核')
                    $('#myModal-temporaryProjectAuditDetails .modal-footer').removeClass('hidden')
                }else if(obj.data.status == 1){
                    $('#status').text('通过')
                    $('#myModal-temporaryProjectAuditDetails .modal-footer').addClass('hidden')
                    $('#note').val(obj.data.note)
                }else if(obj.data.status == 2){
                    $('#status').text('未通过')
                    $('#myModal-temporaryProjectAuditDetails .modal-footer').addClass('hidden')
                    $('#note').val(obj.data.note)
                }
                $('#serviceCharge').text(obj.data.serviceCharge)
                $('#personNumber').text(obj.data.personNum)
                $('#content').val(obj.data.content)
                var tbody = $('.temporaryProjectManagerDetails-table-selfDefine tbody')
                for(var i = 0; i < obj.data.details.length; i++){
                    var appendStr ="<tr class='table-tr'><td>" + (i+1) +"</td><td>" + obj.data.details[i].user.name +"</td>" +
                        "<td>" + obj.data.details[i].unitWage + "</td><td>" + obj.data.details[i].days +"</td>" +
                        "<td style='border-right: none'>" + obj.data.details[i].sumWage + "</td></tr>"
                    tbody.append(appendStr)
                }
                $('#totalWage').text(obj.data.totalWage)
                $('#auditor2').text(obj.data.auditor.name)
                $('#auditor2').attr('value', obj.data.auditor.id)
            }else{
                alert(obj.message)
            }
        },
        error: function (error) {
            console.log(error)
        }
    })
}
/*
提交审核/
 */
function submitAudit(thisObj) {
    var id = $('#makeTablePerson').attr('value')
    var auditor = $('#auditor2').attr('value')
    var note = $('#note').val()
    $.ajax({
        url: ipPort + '/temporaryProjectHeader/auditById?id=' + id + '&auditorId=' + auditor + '&status=' + thisObj + '&note=' + note,
        type: 'post',
        success: function (obj) {
            if(obj.code == 0){
                alert('审核成功！')
                getAllTemporaryApply()
                $('#myModal-temporaryProjectAuditDetails').modal('toggle')
            }else{
                alert(obj.message)
            }
        },
        error: function (error) {
            console.log(error)
        }
    })
}
/**********************************************************888/
 /*
 上一页/
 */
function previousPage() {
    var currentPage_ = $('.currentPage').text()
    if(currentPage_ == 1){
        alert("已经是第一页！")
        return
    }
    currentPage--
    if(currentPage < 0){
        currentPage = 0
    }
    if(currentSearch == -1){
        getAllTemporaryApply(currentPage)
    }else  if(currentSearch == -2){
        searchByStatus(currentPage)
    }else if(currentSearch == -3){
        initTemporaryApply(currentPage)
    }
}
/*
下一页/
 */
function nextPage() {
    var currentPage_ = $('.currentPage').text()
    var totalPage_ = $('.totalPage').text()
    if(currentPage_ == totalPage_){
        alert("已经是最后一页！")
        return
    }
    currentPage++
    if(currentSearch == -1){
        getAllTemporaryApply(currentPage)
    }else  if(currentSearch == -2){
        searchByStatus(currentPage)
    }else if(currentSearch == -3){
        initTemporaryApply(currentPage)
    }
}
/*
跳转页/
 */
function skipPage() {
    var skipPage_ = parseInt($('.skipPage').val())
    var totalPage_ = parseInt($('.totalPage').text())
    if(skipPage_ - totalPage_ > 0){
        alert("没有此页！")
        return
    }
    if(skipPage_ < 1){
        alert("没有此页！")
        return
    }
    currentPage = skipPage_ - 1
    if(currentSearch == -1){
        getAllTemporaryApply(currentPage)
    }else  if(currentSearch == -2){
        searchByStatus(currentPage)
    }else if(currentSearch == -3){
        initTemporaryApply(currentPage)
    }
}